//
//  ViewController.swift
//  BasketballScorer
//
//  Created by hasx on 2023/10/27.
//

import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    var showCollectionV:UICollectionView?
    /// 屏幕宽度
    let kScreenWidth = UIScreen.main.bounds.width
    /// 屏幕高度
    let kScreenHeight = UIScreen.main.bounds.height
    
    let titleArr = ["Match Start","Match List","Distinguished Guest","Participation Record","Match Setup","About Us"]
    let imgArr = ["start","list","join","No1","set","about"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.red;
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 4
        layout.minimumInteritemSpacing = 5
        let w:CGFloat = (kScreenWidth - 4*2)/2
        let h:CGFloat = 200
        layout.itemSize = .init(width: w, height: h)
        showCollectionV = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        showCollectionV?.delegate = self
        showCollectionV?.dataSource = self
        showCollectionV?.register(UINib(nibName: "HomeCollectionCell", bundle: nil), forCellWithReuseIdentifier: "HomeCollectionCell")
        showCollectionV!.register(CustomHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderView")

        self.view.addSubview(showCollectionV!)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return titleArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCell", for: indexPath) as? HomeCollectionCell
        cell?.updateCellUiMethod(img: imgArr[indexPath.row], titleStr: titleArr[indexPath.row],index: indexPath.row)
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexPath) as! CustomHeader
            // 配置头部视图的内容
            return headerView
        }
        return UICollectionReusableView()
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            let startVC = StartVC()
            startVC.title = "Start Time"
            self.navigationController?.pushViewController(startVC, animated: true)
        }else if(indexPath.row == 1){
            let joinVC = JoinListVC()
            joinVC.title = "Join Team"
            self.navigationController?.pushViewController(joinVC, animated: true)
        }
    }

}

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 200)
    }
    
    
}

